use lib "../BuildTool/";
use BuildTool;


use Test::More tests => 7;
use Test::Exception;
use Cwd;
 
 BEGIN {

 	use_ok(BuildTool);
 }


subtest 'Test Case 6.1' => sub {
	plan tests => 2;
####################################################################################
#	
# Verifying that the working copy was created, its files deleted and the correct 
# files exported.
#
####################################################################################
	my $first_build = "false";
	my $first_cron = "false";
	my $vcs_path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/TS6/";
	my %build_file_rev = ('http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/all_var.c' => ['9956'],
						  'http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_system/pack.h' => ['9950']
	);
	my $build_file_rev_ref = \%build_file_rev;
	my $test_directory = getcwd();
	BuildTool->checkOut($build_file_rev_ref,$vcs_path,$first_build,$first_cron);
	chdir($test_directory);
		
	is(-e "trunk/quals/main_fw/code/shared_app/all_var.c","1", "Verifying that the file \"all_var.c\" exists");
	is(-e "trunk/quals/main_fw/code/shared_system/pack.h","1", "Verifying that the file \"pack.h\" exists");
	system("rm -rf trunk");
};

subtest 'Test Case 6.2' => sub {
	plan tests => 2;
####################################################################################
#	
# Verifying that the  build fails if present compile or linker errors
####################################################################################

#------------------------------------------------------------------------
# Compiler Errors
#------------------------------------------------------------------------
	my $vcs_path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/TS6/TS6_comp_error/trunk";
	VCS->coFirstBuild($vcs_path);
	my $code_dir = "trunk";
	my $build_name = "fw_builds";
	my $buildInfo = "buildInfo.txt";
	my $conf = "Debug";
	my $type = "cron";
	my $type = "2.2";
	
	Build->setBuildProper($type,$conf,$number,$build_name);
	Build->buildCode("trunk");

	#Verify that the tool will die when there're compiler errors	
	dies_ok{Build->analyzeCompLinkEW($base_warn_surpress,$buildInfo, $build_report_path,$buildReport)}, 'Verifying that the tool dies when there are compile errors';
	system("rm -rf trunk");
	system("rm -rf buildInfo.txt");
	
#------------------------------------------------------------------------
# Linker Errors
#------------------------------------------------------------------------
	my $vcs_path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/TS6/TS6_linker_error/trunk";
	VCS->coFirstBuild($vcs_path);
	my $code_dir = "trunk";
	my $build_name = "fw_builds";
	my $buildInfo = "buildInfo.txt";
	my $conf = "Release";
	my $type = "baseline";
	my $type = "2.1";
	
	Build->setBuildProper($type,$conf,$number,$build_name);
	Build->buildCode("trunk");

	#Verify that the tool will die when there're linker errors	
	dies_ok{Build->analyzeCompLinkEW($base_warn_surpress,$buildInfo, $build_report_path,$buildReport)}, 'Verifying that the tool dies when there are linker errors';
	system("rm -rf trunk");
	system("rm -rf buildInfo.txt");
	

};

subtest 'Test Case 6.3' => sub {
	plan tests => 2;
#########################################################################################
#	
# Verifying that the build lives if present compile or linker warnings, it is a baseline
# and the flag is enabled
#
#########################################################################################

#------------------------------------------------------------------------
# Compiler Warnings
#------------------------------------------------------------------------
	my $vcs_path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/TS6/TS6_comp_warn/trunk";
	VCS->coFirstBuild($vcs_path);
	my $code_dir = "trunk";
	my $build_name = "fw_builds";
	my $buildInfo = "buildInfo.txt";
	my $conf = "Debug";
	my $base_warn_surpress = "true";
	my $type = "baseline";
	my $number = "2.1";
	
	Build->setBuildProper($type,$conf,$number,$build_name);
	Build->buildCode("trunk");
	my $buildReport = "buildReport.txt";
	my $build_report_path = $code_dir . "/" . $build_name . "/" . $conf . "/" . $buildReport;
	open(FILE,">$buildReport");
	print FILE "creating necessary file";
	close FILE;
	#Verify that the tool will live because the flag is enabled	
	lives_ok{Build->analyzeCompLinkEW($base_warn_surpress,$buildInfo, $build_report_path,$buildReport)}, 'Verifying that the tool lives';
	system("rm -rf trunk");
	system("rm -rf buildInfo.txt");


#------------------------------------------------------------------------
# Linker Warnings
#------------------------------------------------------------------------
	my $vcs_path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/TS6/TS6_linker_warn/trunk";
	VCS->coFirstBuild($vcs_path);
	my $code_dir = "trunk";
	my $build_name = "fw_builds";
	my $buildInfo = "buildInfo.txt";
	my $conf = "Release";
	my $base_warn_surpress = "true";
	my $type = "baseline";
	my $number = "2.1";
	Build->setBuildProper($type,$conf,$number,$build_name);
	Build->buildCode("trunk");
	#Verify that the tool will live because the flag is enabled	
	lives_ok{Build->analyzeCompLinkEW($base_warn_surpress,$buildInfo, $build_report_path,$buildReport)}, 'Verifying that the tool lives';
	system("rm -rf trunk");
	system("rm -rf buildInfo.txt");
	

};

subtest 'Test Case 6.4' => sub {
	plan tests => 2;
#########################################################################################
#	
# Verifying that the build fails if present compile or linker warnings, it is a baseline
# and the flag not is enabled
#
#########################################################################################

#------------------------------------------------------------------------
# Compiler Warnings
#------------------------------------------------------------------------
	my $vcs_path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/TS6/TS6_comp_warn/trunk";
	VCS->coFirstBuild($vcs_path);
	my $code_dir = "trunk";
	my $build_name = "fw_builds";
	my $buildInfo = "buildInfo.txt";
	my $conf = "Debug";
	my $base_warn_surpress = "false";
	my $type = "baseline";
	my $number = "2.1";
	
	Build->setBuildProper($type,$conf,$number,$build_name);
	Build->buildCode("trunk");
	my $buildReport = "buildReport.txt";
	my $build_report_path = $code_dir . "/" . $build_name . "/" . $conf . "/" . $buildReport;
	open(FILE,">$buildReport");
	print FILE "creating necessary file";
	close FILE;
	#Verify that the tool will die because the flag it not enabled 	
	dies_ok{Build->analyzeCompLinkEW($base_warn_surpress,$buildInfo, $build_report_path,$buildReport)}, 'Verifying that the tool dies';
	system("rm -rf trunk");
	system("rm -rf buildInfo.txt");


#------------------------------------------------------------------------
# Linker Warnings
#------------------------------------------------------------------------
	my $vcs_path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/TS6/TS6_linker_warn/trunk";
	VCS->coFirstBuild($vcs_path);
	my $code_dir = "trunk";
	my $build_name = "fw_builds";
	my $buildInfo = "buildInfo.txt";
	my $conf = "Release";
	my $base_warn_surpress = "false";
	my $type = "baseline";
	my $number = "2.1";
	Build->setBuildProper($type,$conf,$number,$build_name);
	Build->buildCode("trunk");
	#Verify that the tool will die when there're linker warnings and the flag is not enabled	
	dies_ok{Build->analyzeCompLinkEW($base_warn_surpress,$buildInfo, $build_report_path,$buildReport)}, 'Verifying that the tool dies';
	system("rm -rf trunk");
	system("rm -rf buildInfo.txt");
	system("rm -rf buildReport.txt");
	
};

subtest 'Test Case 6.5' => sub {
	plan tests => 2;
#########################################################################################
#	
# Verifying that the build fails if present compile or linker warnings, it is a baseline
# and the flag not is enabled
#
#########################################################################################

#------------------------------------------------------------------------
# Compiler Warnings
#------------------------------------------------------------------------
	my $vcs_path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/TS6/TS6_comp_warn/trunk";
	VCS->coFirstBuild($vcs_path);
	my $code_dir = "trunk";
	my $build_name = "fw_builds";
	my $buildInfo = "buildInfo.txt";
	my $conf = "Debug";
	my $base_warn_surpress = "false";
	my $type = "cron";
	my $number = "2.2";
	
	Build->setBuildProper($type,$conf,$number,$build_name);
	Build->buildCode("trunk");
	my $buildReport = "buildReport.txt";
	my $build_report_path = $code_dir . "/" . $build_name . "/" . $conf . "/" . $buildReport;
	open(FILE,">$buildReport");
	print FILE "creating necessary file";
	close FILE;
	#Verify that the tool will live even with the flag disabled because the build is a cron	
	lives_ok{Build->analyzeCompLinkEW($base_warn_surpress,$buildInfo, $build_report_path,$buildReport)}, 'Verifying that the tool lives';
	system("rm -rf trunk");
	system("rm -rf buildInfo.txt");


#------------------------------------------------------------------------
# Linker Warnings
#------------------------------------------------------------------------
	my $vcs_path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/TS6/TS6_linker_warn/trunk";
	VCS->coFirstBuild($vcs_path);
	my $code_dir = "trunk";
	my $build_name = "fw_builds";
	my $buildInfo = "buildInfo.txt";
	my $conf = "Release";
	my $base_warn_surpress = "false";
	my $type = "cron";
	my $number = "2.2";
	Build->setBuildProper($type,$conf,$number,$build_name);
	Build->buildCode("trunk");
	#Verify that the tool will live even with the flag disabled becaus the build is a cron	
	lives_ok{Build->analyzeCompLinkEW($base_warn_surpress,$buildInfo, $build_report_path,$buildReport)}, 'Verifying that the tool lives';
	system("rm -rf trunk");
	system("rm -rf buildInfo.txt");
	
	
};

subtest 'Test Case 6.6' => sub {
	plan tests => 1;
#########################################################################################
#	
# Verifying that the build lives when there are no compiler/linker errors and 
# warnings.
#
#########################################################################################


	my $vcs_path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/TS6/TS6_clean_code/trunk";
	VCS->coFirstBuild($vcs_path);
	my $code_dir = "trunk";
	my $build_name = "fw_builds";
	my $buildInfo = "buildInfo.txt";
	my $conf = "Debug";
	my $base_warn_surpress = "false";
	my $type = "baseline";
	my $number = "2.1";
	
	Build->setBuildProper($type,$conf,$number,$build_name);
	Build->buildCode("trunk");
	my $buildReport = "buildReport.txt";
	my $build_report_path = $code_dir . "/" . $build_name . "/" . $conf . "/" . $buildReport;
	open(FILE,">$buildReport");
	print FILE "creating necessary file";
	close FILE;
	#Verify that the tool will live even with the flag disabled because the build is a cron	
	lives_ok{Build->analyzeCompLinkEW($base_warn_surpress,$buildInfo, $build_report_path,$buildReport)}, 'Verifying that the tool lives';
	system("rm -rf trunk");
	system("rm -rf buildInfo.txt");
	system("rm -rf buildReport.txt");

};
